﻿using AlibabaCloud.SDK.Dysmsapi20170525.Models;

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

using Tea;
using Tea.Utils;

namespace ZFB.Common
{
    public static class AliyunSmsHelper
    {

        /// <summary>
        ///     阿里云访问密钥
        /// </summary>
        public static string AccessKeyId = "LTAI5t7EXHuvKjTJpJoCH3hY"; //用户标识
        public static string AccessKeySecret = "JEf88kXLeJTOPoAPzhBdO92rrYs1Qs"; //用户密钥

        /**
  * 使用AK&SK初始化账号Client
  * @param accessKeyId
  * @param accessKeySecret
  * @return Client
  * @throws Exception
  */
        public static AlibabaCloud.SDK.Dysmsapi20170525.Client CreateClient(string accessKeyId, string accessKeySecret)
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // 必填，您的 AccessKey ID
                AccessKeyId = accessKeyId,
                // 必填，您的 AccessKey Secret
                AccessKeySecret = accessKeySecret,
            };
            // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
            config.Endpoint = "dysmsapi.aliyuncs.com";
            return new AlibabaCloud.SDK.Dysmsapi20170525.Client(config);
        }

        public static string SendSms(string PhoneNumbers,string SignName,string TemplateCode,string TemplateParam)
        {
            // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
            // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用，仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378671.html
            AlibabaCloud.SDK.Dysmsapi20170525.Client client = CreateClient(AccessKeyId, AccessKeySecret);
            AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest sendSmsRequest = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest
            {
                SignName = SignName,
                TemplateCode = TemplateCode,
                PhoneNumbers = PhoneNumbers,
                TemplateParam = TemplateParam,
            };
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            var a = client.SendSmsWithOptions(sendSmsRequest, runtime);
            return a.Body.ToJson();
            //try
            //{
            //    复制代码运行请自行打印 API 的返回值
            //var a = client.SendSmsWithOptions(sendSmsRequest, runtime);
            //    return a.StatusCode.Value;
            //}
            //catch (TeaException error)
            //{
            //    如有需要，请打印 error
            //    AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
            //}
            //catch (Exception _error)
            //{
            //    TeaException error = new TeaException(new Dictionary<string, object>
            //    {
            //        { "message", _error.Message }
            //    });
            //    如有需要，请打印 error
            //    AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
            //}
        }


    }

}

